/**
 * 图片上传路径替换规则
 */

import urlConfig from '@/urlConfig'
const baseImageUrl = urlConfig.baseURL

export default {
  methods: {
    /**
     * @param {imgUrl} 图片路径替换
     */
    imgUrlReplace(imgUrl) {
      if (!imgUrl) return
      if (imgUrl.indexOf('minio/image') == -1) return imgUrl
      const fileName = imgUrl.split('/minio/image/testfile/')
      return `${baseImageUrl}/minio/image/testfile/${fileName[1]}`
    },
    /**
     * 富文本图片路径 转换
     */
    // <video src="http://192.168.100.47:9115/minio/image/testfile/1637737635914_movie.mp4" controls="controls" style="max-width:100%"></video>
    richTxtUrlReplace(str) {
      if (!str) return ''
      const imageFilterStr = str.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, (match, url, index, str) => {
        return match.replace(url, this.imgUrlReplace(url))
      })

      const videoFilterStr = imageFilterStr.replace(/<video [^>]*src=['"]([^'"]+)[^>]*><\/video>/gi, (match, url) => {
        return match.replace(url, this.imgUrlReplace(url))
      })
      return videoFilterStr
    }
  }
}
